package com.whp.yky.yky.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.whp.yky.yky.entity.poentity.Datas;
import com.whp.yky.yky.dao.DataMapper;
import com.whp.yky.yky.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataServiceImpl implements DataService {


    @Autowired
    private DataMapper dataMapper;





    @Override
    public IPage<Datas> getDataInfo(IPage<Datas> page,Datas datas){
        return page.setRecords(dataMapper.getDataInfo(page,datas));
    }


    @Override
    public boolean insertDataInfo(Datas datas){



            try {
                int effecteNum= dataMapper.insertDataInfo(datas);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("增加成功，主键为："+ datas.getId());
                    return true;
                }else {
                    throw  new RuntimeException("插入信息失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("插入信息失败了:"+e.getMessage());
            }


    }

    @Override
    public boolean updateDataInfo(Datas datas){

        if (datas.getId()!= 0&&!"".equals(datas.getId())){

            try {
                int effecteNum= dataMapper.updateDataInfo(datas);
                if(effecteNum>0){
                    //如果影响行数大于0，那么就是增加成功
                    System.out.println("修改成功，主键为："+ datas.getId());
                    return true;
                }else {
                    throw  new RuntimeException("修改失败,插入行数有误");
                }
            }catch (Exception e){
                throw  new RuntimeException("修改失败:"+e.getMessage());
            }
        }else{
            throw new RuntimeException("信息不能为空！！！！");
        }

    }


    @Override
    public boolean deleteDataInfo(int id) {

        try {
            //对mysql数据库做删除操作后，影响的行数effecteNum
            int effecteNum= dataMapper.deleteDataInfo(id);
            if (effecteNum>0){
                return true;
            }else {
                return false;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public Datas getDataInfoById(int id) {
        return dataMapper.getDataInfoById(id);
    }



}
